//
//  BleControlViewController.m
//  StormWatch
//
//  Created by 王岩 on 2024/8/31.
//

#import "BleControlViewController.h"
#import "BleControlInfoCell.h"
#import "BleControlFunctionCell.h"
#import "BleOTAViewController.h"
#import "StepTargetViewController.h"
#import "HeartRateReminderViewController.h"
#import "AlarmListViewController.h"
#import "MessageNotificationViewController.h"
#import "FRIDefine.h"


@interface BleControlViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView * collectionView;
@property (nonatomic, strong) NSArray * dataArr, * imageArr;

@end

@implementation BleControlViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backView) name:FRINotificationAtDidDisconnectPeripheral object:nil];

    self.dataArr = @[SWString(@"Alarm"), SWString(@"Step goal"),SWString(@"Scheduled heart rate reminders"),SWString(@"Sedentary reminder"),SWString(@"Screen time"),SWString(@"Raise hand to brighten screen"),SWString(@"Notification"),SWString(@"OTA upgrade"),SWString(@"Dial switch")];
    
    self.imageArr = @[@"icon_device_alarm",@"icon_device_step_target",@"icon_device_time_heart",@"icon_device_long_sit",@"icon_device_brighten",@"icon_device_hand_brighten",@"icon_device_notification",@"icon_device_ota",@"icon_device_theme_gray"];
    
    [self.view addSubview:self.collectionView];
    [self readData];
    // Do any additional setup after loading the view.
}

- (void)readData {
    
//    if (! FRQBluetoothManager.shareManager.curPeripheral.macAddress.length) {
        [StormCommandTool.sharedInstance writeStormCommand:Command_DeviceInfo Value:nil reBack:^(NSString * _Nonnull backData) {
            if (backData.length > 26 + 22) {
                FRQBluetoothManager.shareManager.curPeripheral.macAddress = [backData substringWithRange:NSMakeRange(26 +10, 12)];
                [self.collectionView reloadData];
            }
            
            [[StormCommandTool sharedInstance] readRealTimeData:^(BOOL success, unsigned long stepData, unsigned long caloriesData, unsigned long distanceData, unsigned long heartRateData, unsigned long electricData) {
                [self.collectionView reloadData];
            }];
            
        }];
//    }

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    return self.dataArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        BleControlInfoCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BleControlInfoCell" forIndexPath:indexPath];
        [cell refreshUI];
        cell.disBlock = ^{
            [FRQBluetoothManager shareManager].userDisconnect = YES;
            [[FRQBluetoothManager shareManager] closeAuotReconnectPeripheral:[FRQBluetoothManager shareManager].curPeripheral];
        };
        
        return cell;
    }else {
        BleControlFunctionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BleControlFunctionCell" forIndexPath:indexPath];
        cell.nameLab.text = self.dataArr[indexPath.row];
        cell.iconView.image = S_imageNamed(self.imageArr[indexPath.row]);
        return cell;
    }
   
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(kScreenWidth - 20, 180);
    }
    return CGSizeMake(kScreenWidth - 20, 70);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return;
    }
   
    if (indexPath.row == 0) {
        AlarmListViewController * vc= [[AlarmListViewController alloc] init];
        [self.navigationController pushViewController:vc
                                             animated:YES];
    }else if (indexPath.row == 1 ){
        StepTargetViewController * vc= [[StepTargetViewController alloc] initWithNibName:@"StepTargetViewController" bundle:nil];
        vc.type = Command_StepGoal;
        [self.navigationController pushViewController:vc
                                             animated:YES];
    }else if (indexPath.row == 4) {
        StepTargetViewController * vc= [[StepTargetViewController alloc] initWithNibName:@"StepTargetViewController" bundle:nil];
        vc.type = Command_ScreenTime;
        [self.navigationController pushViewController:vc
                                             animated:YES];
    }else if (indexPath.row == 2) {
        HeartRateReminderViewController* vc= [[HeartRateReminderViewController alloc] initWithNibName:@"HeartRateReminderViewController" bundle:nil];
        vc.type = Command_HeartRateReminders;
        [self.navigationController pushViewController:vc
                                             animated:YES];
    }else if (indexPath.row == 3) {
        HeartRateReminderViewController* vc= [[HeartRateReminderViewController alloc] initWithNibName:@"HeartRateReminderViewController" bundle:nil];
        vc.type = Command_Sedentary;
        [self.navigationController pushViewController:vc
                                             animated:YES];
    }else if (indexPath.row == 5) {
        HeartRateReminderViewController* vc= [[HeartRateReminderViewController alloc] initWithNibName:@"HeartRateReminderViewController" bundle:nil];
        vc.type = Command_RaiseHand;
        [self.navigationController pushViewController:vc
                                             animated:YES];
    }else if (indexPath.row == 6){
        MessageNotificationViewController * vc = [[MessageNotificationViewController alloc] init];
        [self.navigationController pushViewController:vc
                                             animated:YES];
    }else if (indexPath.row == 7){
        BleOTAViewController * vc = [[BleOTAViewController alloc] init];
        [self.navigationController pushViewController:vc
                                             animated:YES];
    }else{
        toast(SWString(@"Developing..."));
    }
    

}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 10;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 0, kScreenWidth - 20, kScreenHeight - kNavigationBarHeight - kHomeIndicatorHeight) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor clearColor];
        [_collectionView registerNib:[UINib nibWithNibName:@"BleControlFunctionCell" bundle:nil] forCellWithReuseIdentifier:@"BleControlFunctionCell"];
        [_collectionView registerNib:[UINib nibWithNibName:@"BleControlInfoCell" bundle:nil] forCellWithReuseIdentifier:@"BleControlInfoCell"];
    }
    return _collectionView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
